<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 05/01/2019
 * Time: 16:32
 */

namespace App\Modules\Index;


use App\Models\User\User;
use App\Modules\AdapterApi;
use App\Services\Index\IndexIndex;
use App\Services\User\UserIndex;

class Index extends AdapterApi
{
    /**
     * 用户登录
     * @return array
     */
    public function login(): array
    {
        // 接收错误次数和检验图形验证码参数
        $data = pick_array($this->postData, ['name', 'login_password', 'token', 'img_type', 'img_code_id', 'auth_code', 'is_auto_login', 'token', 'phone', 'platform']);

        // 获取token内容
        $tokenContent = UserIndex::Token()->hGetAll($data['token']);
        if ($tokenContent['status'] == SUCCESS_STATUS && isset($tokenContent['res']['id'])) {
            foreach ($tokenContent['res'] as $k => &$v) {
                if (in_array($k, ['fund_etc', 'county_text', 'city_text', 'province_text', 'other_info', 'enterprise_info']))
                    $v = json_decode($v, true);
            }
            return auto_return($tokenContent, '成功获取用户信息', '获取用户信息失败');
        }

//        if ($tokenContent['res']['no_user_err_counts'] >= MAX_LOGIN_ERR_TIMES) {
//            $checkAuthCode = $this->_checkAuthCode($data);
//            if ($checkAuthCode['status'] != SUCCESS_STATUS) {
//                return verify_err($tokenContent['res'], '验证图形验证码失败');
//            }
//        }

        //根据name获取用户信息中的login_err_times,用于判断是否检验图形验证码
        $baseInfo = UserIndex::User()->info(['phone'=>$data['phone'],'is_add_no_user_counts'=>1,'token'=>$data['token']]);
        if($baseInfo['status'] != SUCCESS_STATUS){
            $tokenContent['res']['no_user_err_counts'] = $tokenContent['res']['no_user_err_counts'] + 1;
            return verify_err($tokenContent['res'], '没有相关用户信息');
        }

        //判断：若次数大于所允许的错误登录次数：验证图形验证码
//        if($baseInfo['res']['login_err_times'] >= MAX_LOGIN_ERR_TIMES && !isset($checkAuthCode)){
//            $checkAuthCode = $this->_checkAuthCode($data);
//            if($checkAuthCode['status'] != SUCCESS_STATUS){
//                $checkAuthCode['res']['last_err_login_time'] = $baseInfo['res']['last_err_login_time'];
//                $checkAuthCode['res']['login_err_times'] = $baseInfo['res']['login_err_times'];
//                $checkAuthCode['res']['max_login_err_times'] = MAX_LOGIN_ERR_TIMES;
//                $checkAuthCode['res']['no_user_err_counts'] = $tokenContent['res']['no_user_err_counts'];
//                return $checkAuthCode;
//            }
//        }

        //判断用户登录平台是否与角色一致
        $uMod = User::where('phone', $data['phone'])->first();
        switch ($data['platform']) {
            case 'driver':
                if(!in_array(164, explode(',', $uMod->role_ids)) && !in_array(166, explode(',', $uMod->role_ids)))
                    return auto_failure(null, '您无权登录该平台');
                break;
            case 'owner':
                if(!in_array(163, explode(',', $uMod->role_ids)))
                    return auto_failure(null, '您无权登录该平台');
                break;
        }

        //登录并获取用户信息 若第一次打勾自动登录 返回auto_login_token信息
        $loginInfoRes = UserIndex::User()->login($data);
        if($loginInfoRes['status'] != SUCCESS_STATUS){
            return verify_err($loginInfoRes['res'], $loginInfoRes['msg']);
        }

        //获取用户的资金信息  不可在service里的登录方法中验证
//        $fundInfo = FundIndex::FundAccount()->userFund(['user_id'=>$baseInfo['res']['id']]);
//        if($fundInfo['status'] != SUCCESS_STATUS){
//            return $fundInfo;
//        }

        //组装用户信息
//        $allInfo = array_merge($loginInfoRes['res'], $fundInfo['res']);
//        $allInfo && $allInfo['earnings'] = 0;
        return auto_return($loginInfoRes, '成功获取用户信息', '获取用户信息失败');

    }

    //退出登录
    public function logout()
    {
        $data = pick_array($this->postData, ['token']);
        return UserIndex::User()->logout($data);
    }

    // 用户注册
    public function register(): array
    {
        $data = pick_array($this->postData, ['phone', 'code', 'login_password', 'type', 'name', 'other_info', 'token']);
        $res = UserIndex::User()->register($data);

        if ($res['status'] == SUCCESS_STATUS) {
            return auto_success($res, '注册成功');
        }else{
            return auto_failure($res, $res['msg']);
        }
    }

    public function sendCode(): array
    {
        $data = pick_array($this->postData, ['phone']);
        return IndexIndex::Index()->sendCode($data);
    }
}
